//
//  XS_VIPLogVC.swift
//  XueDao
//
//  Created by Mac on 2021/2/2.
//

import UIKit

class XS_VIPLogVC: XS_BaseVC {

    var vipLogRecordModel = XS_VIPLogItemM()
    
    @IBOutlet weak var collectionView: UICollectionView!
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        title = "购买记录"
        
        shouldNavBarHidden = false
        collectionView.contentInset = UIEdgeInsets.init(top: 0, left: 0, bottom: .bottomHeight + 16, right: 0)
        collectionView.register(UINib.init(nibName: "XS_VIPLogItemCell", bundle: Bundle.main), forCellWithReuseIdentifier: "XS_VIPLogItemCell")
        collectionView.delegate = self
        collectionView.dataSource = self
        
        collectionView.mj_header = MJRefreshNormalHeader.init(refreshingTarget: self, refreshingAction: #selector(headerRefesh))
        collectionView.mj_footer = MJRefreshBackNormalFooter.init(refreshingTarget: self, refreshingAction: #selector(footerRefesh))
        collectionView.mj_footer?.ignoredScrollViewContentInsetBottom = (UIApplication.shared.statusBarFrame.size.height == 44) ? 34 : 0
        headerRefesh()
    }
    
    @objc func headerRefesh(){
        requestData(true)
    }
    
    @objc func footerRefesh(){
        requestData(false)
    }

    func requestData(_ isFirst: Bool = false){
        if isFirst {
            vipLogRecordModel.current = 0
            vipLogRecordModel.records.removeAll()
        }
        XS_NetWork.requestJson("/vip/getVipList", parameters: ["pageNo" : vipLogRecordModel.current+1, "pageSize" : vipLogRecordModel.size])
            .mapModel(type: XS_VIPLogItemM.self) // 返回 模型数组 和原始数据
            .subscribe(onNext: { [weak self] model, resp in
                guard let `self` = self, resp.success, let model = model else { return }
                model.records = self.vipLogRecordModel.records + model.records
                self.vipLogRecordModel = model
                if model.records.count > 0 {
                    XD_BlankPageView.hiddenBlankPageView(superView: self.collectionView)
                    self.collectionView.reloadData()
                } else {
                    XD_BlankPageView.blankPage(superView: self.collectionView, imgName: "缺省页_暂无内容", detailStr: "暂无内容~", bottomDetailStr: "")
                }
                self.collectionView.mj_header?.endRefreshing()
                if model.current >= model.pages { // 没有更多
                    self.collectionView.mj_footer?.endRefreshingWithNoMoreData()
                }else{
                    self.collectionView.mj_footer?.endRefreshing()
                }
            }).disposed(by: disposeBag)
    }

}

extension XS_VIPLogVC : UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return vipLogRecordModel.records.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "XS_VIPLogItemCell", for: indexPath) as! XS_VIPLogItemCell
        if indexPath.item < vipLogRecordModel.records.count {
            cell.model = vipLogRecordModel.records[indexPath.item]
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize.init(width: .screenWidth - 32, height: 106)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets.init(top: 16, left: 16, bottom: 16, right: 16)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 16
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 16
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
    }
}

